<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>注册页面</h1>
<form action="">
    <input type="text" name="username" placeholder="用户名" @blur="blurfn()">
    <span>{{info}}</span><br>
    <input type="text" name="password" placeholder="密码"><br>
    <input type="text" name="nick" placeholder="昵称"><br>
    <input type="text" name="age" placeholder="年龄"><br>
    <input type="button" value="注册" @click="reg()">
</form>
<!--引入axios框架-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!--引入vue框架-->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    let v=new Vue({
        el:"form",
        data:{
            info:""
        },
        methods:{
            blurfn:function () {
               let data =new FormData(document.querySelector("form"));
               axios.post("/check",data).then(function (response) {
                   v.info=response.data;
               })
            },
            reg:function () {
                let data =new FormData(document.querySelector("form"));
                axios.post("/reg",data).then(function (response) {
                    //如果注册成功服务器返回1，如果用户名已存在用户名返回0
                    if(response==1){
                        alert("注册成功！");
                        //让浏览器显示首页 类似之前的重定向
                        location.href="/index.html";
                    }else {
                        alert("用户名已存在！")
                    }
                })
            }

        }
    })
</script>
</body>
</html>